package com.webdrider.servlet.action.snippet;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.model.validators.ActionValidator;
import com.common.security.abstracts.AbstractError;
import com.common.security.abstracts.AbstractException;
import com.common.security.errors.ParserError;
import com.common.security.exceptions.ManagerException;
import com.common.security.exceptions.ParserException;
import com.common.servlet.abstracts.AbstractParserServlet;
import com.common.servlet.interfaces.IParser;
import com.webdrider.manager.action.snippet.DomainManager;
import com.webdrider.manager.interfaces.IDomainManager;
import com.webdrider.model.action.snippet.Domain;
import com.webdrider.model.requests.snippet.GetAllSnippetDomainsRequest;
import com.webdrider.model.response.snippet.GetAllSnippetDomainsResponse;
import com.webdrider.model.validators.SessionValidator;
import com.webdrider.security.errors.SnippetError;
import com.webdrider.servlet.util.RequestAction;
import com.webdrider.servlet.util.parser.snippet.GetAllSnippetDomainsXMLParser;

public class GetAllSnippetDomainsServlet extends AbstractParserServlet {
	private static final long serialVersionUID = 1L;
	private static Set<AbstractValidator> validators = new HashSet<AbstractValidator>();
	
	private GetAllSnippetDomainsRequest requestContent;
	private GetAllSnippetDomainsXMLParser parser = new GetAllSnippetDomainsXMLParser();
	private IDomainManager manager = new DomainManager();
	
	static {
		validators.add(new ActionValidator(RequestAction.GET_ALL_SNIPPET_DOMAINS));
		validators.add(new SessionValidator());
	}

	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response) {
		try {
			GetAllSnippetDomainsResponse allSnippetDomainsResponse = new GetAllSnippetDomainsResponse();
			List<Domain> domains = manager.getAll();
			
			allSnippetDomainsResponse.setRequestAction(requestContent.getAction());
			allSnippetDomainsResponse.setDomains(domains);
			
			response.setContentType("text/xml");
			response.getWriter().write(parser().parseResponse(allSnippetDomainsResponse));
			response.getWriter().close();
		} catch (ManagerException managerException) {
			exception(managerException, response);
		} catch (ParserException parserException) {
			exception(parserException, response);
		} catch (IOException ioException) {
			error(new ParserError(ParserError.STREAM_ERROR), response);
		}
	}

	@Override
	public void setRequest(AbstractRequest abstractRequest) {
		requestContent = (GetAllSnippetDomainsRequest) abstractRequest;
	}

	@Override
	public Set<AbstractValidator> getValidators() {
		return validators;
	}

	@Override
	public IParser parser() {
		return parser;
	}
	
	@Override
	public void exception(AbstractException exception, HttpServletResponse response) {
		super.exception(exception, response);
		
		switch (exception.getCode()) {
			 case ManagerException.DATABASE_EXCEPTION: {
				 error(new SnippetError(AbstractError.DATABASE_ERROR), response);
				 break;
			 }
		}
	}
}
